<!DOCTYPE html>
<html>
<head>
	<title>权限树扩展分享</title>
	<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
	<script type="text/javascript" src="../../layui/layui.js"></script>
</head>
<body>
<div class="layui-container">
	<div class="layui-row">
		<div class="layui-col-md6 layui-col-md-offset1">
			<fieldset class="layui-elem-field layui-field-title"><legend>权限树扩展分享</legend></fieldset>
			<!-- 此扩展能递归渲染一个权限树，点击深层次节点，父级节点中没有被选中的节点会被自动选中，单独点击父节点，子节点会全部 选中/去选中 -->
			<form class="layui-form">
				<div class="layui-form-item">
					<label class="layui-form-label">角色名称</label>
					<div class="layui-input-block">
						<input class="layui-input" type="text" name="name" placeholder="请输入角色名称" />
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">选择权限</label>
					<div class="layui-input-block">
						<div id="LAY-auth-tree-index"></div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" lay-filter="update">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
layui.config({
	base: '../../js/',
}).extend({
	authtree: 'authtree',
});
layui.use(['jquery', 'authtree', 'form'], function(){
	var $ = layui.jquery;
	var authtree = layui.authtree;
	var form = layui.form();
	var authorIds = [];
	$.ajax({
		url: 'tree.json',
		dataType: 'json',
		success: function(data){
			// 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
			authtree.render('#LAY-auth-tree-index', data.data.trees, {inputname: 'authorIds', layfilter: 'lay-check-auth', openall: true});

			// 监听自定义lay-filter选中状态，PS:layui现在不支持多次监听，所以扩展里边只能改变触发逻辑，然后引起了事件冒泡延迟的BUG，要是谁有好的建议可以反馈我
			form.on('checkbox(lay-check-auth)', function(data){
				// 注意这里：需要等待事件冒泡完成，不然获取叶子节点不准确。
				setTimeout(function(){
					// 获取选中的叶子节点
					authorIds = authtree.getLeaf('#LAY-auth-tree-index');
					console.log(authorIds);
				}, 100);
				return false;
			});

		}
	});

	form.on("submit(update)",function(data){
		//弹出loading
		data.field.authorIds=authorIds;
		console.log(data.field);
		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
	/*	$.ajax({
			url:"/author/update.do",
			data:data.field,
			type:"post",
			success:function(data){
				top.layer.close(index);
				top.layer.msg("修改成功！");
				layer.closeAll("iframe");
				//刷新父页面
				parent.location.reload();
			},
			error:function(){
				top.layer.close(index);
				top.layer.msg("修改成功！");
				layer.closeAll("iframe");
				//刷新父页面
				parent.location.reload();
			}
		})*/
		return false;
	})
});


</script>
</html>